#include "studentui.h"
#include "ui_studentui.h"

studentui::studentui(QSqlDatabase* db,QString account,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::studentui)
{
    ui->setupUi(this);

    //初始化成员变量
    m_db = *db;
    m_account = account;

    this->setWindowTitle("欢迎使用学生信息管理系统!@学生"+account);

    ini();
}

studentui::~studentui()
{
    delete ui;
}

void studentui::on_btnQuit_clicked()
{
    this->close();
}

void studentui::ini()
{
    ui->editName->setReadOnly(true);
    ui->editPswd->setReadOnly(true);
    ui->btnReset->setEnabled(false);
    ui->btnSubmit->setEnabled(false);

    QSqlQuery query;
    query.exec("select * from tb_student where id="+m_account+";");
    query.first();
    if(!query.isValid()){
        QMessageBox::warning(0,"警告","数据库检索出错!");
        return;
    }

    ui->labId->setText(m_account);
    ui->labStudent->setText(query.value("name").toString());
    ui->editPswd->setText(query.value("pswd").toString());
    ui->editName->setText(query.value("name").toString());
    ui->labMajor->setText(query.value("major").toString());
    ui->labClass->setText(query.value("class").toString());
    ui->labDegree->setText(query.value("degree").toString());

    //初始化数据模型
    m_evaModel = new  QSqlTableModel(this);
    m_gradeModel = new  QSqlTableModel(this);
    m_testModel = new  QSqlTableModel(this);
    m_choiceCourseModel = new  QSqlTableModel(this);
    m_courseModel = new  QSqlTableModel(this);

    //绑定数据表
    m_evaModel->setTable("tb_evaluate");
    m_gradeModel->setTable("tb_grade");
    m_testModel->setTable("tb_test");
    m_choiceCourseModel->setTable("tb_teach");
    m_courseModel->setTable("tb_stuCourse");

    //设置当主动提交修改时保存数据至数据库
    m_evaModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    m_gradeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    m_testModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    m_choiceCourseModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    m_courseModel->setEditStrategy(QSqlTableModel::OnManualSubmit);

    //自动拉伸视图
    ui->tvEva->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvGrade->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvTest->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvChoiceCourse->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvCourse->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    //设置表头
    m_evaModel->setHeaderData(0,Qt::Horizontal,"编号",Qt::DisplayRole);
    m_evaModel->setHeaderData(1,Qt::Horizontal,"学生学号",Qt::DisplayRole);
    m_evaModel->setHeaderData(2,Qt::Horizontal,"学生名字",Qt::DisplayRole);
    m_evaModel->setHeaderData(3,Qt::Horizontal,"老师编号",Qt::DisplayRole);
    m_evaModel->setHeaderData(4,Qt::Horizontal,"老师名字",Qt::DisplayRole);
    m_evaModel->setHeaderData(5,Qt::Horizontal,"评语",Qt::DisplayRole);
    m_evaModel->setHeaderData(6,Qt::Horizontal,"评分",Qt::DisplayRole);


    m_gradeModel->setHeaderData(0,Qt::Horizontal,"编号",Qt::DisplayRole);
    m_gradeModel->setHeaderData(1,Qt::Horizontal,"学生学号",Qt::DisplayRole);
    m_gradeModel->setHeaderData(2,Qt::Horizontal,"学生名字",Qt::DisplayRole);
    m_gradeModel->setHeaderData(3,Qt::Horizontal,"课程编号",Qt::DisplayRole);
    m_gradeModel->setHeaderData(4,Qt::Horizontal,"课程名称",Qt::DisplayRole);
    m_gradeModel->setHeaderData(5,Qt::Horizontal,"课程类型",Qt::DisplayRole);
    m_gradeModel->setHeaderData(6,Qt::Horizontal,"分数",Qt::DisplayRole);
    m_gradeModel->setHeaderData(7,Qt::Horizontal,"教师编号",Qt::DisplayRole);
    m_gradeModel->setHeaderData(8,Qt::Horizontal,"教师姓名",Qt::DisplayRole);

    m_testModel->setHeaderData(0,Qt::Horizontal,"编号",Qt::DisplayRole);
    m_testModel->setHeaderData(1,Qt::Horizontal,"学生学号",Qt::DisplayRole);
    m_testModel->setHeaderData(2,Qt::Horizontal,"学生名字",Qt::DisplayRole);
    m_testModel->setHeaderData(3,Qt::Horizontal,"课程编号",Qt::DisplayRole);
    m_testModel->setHeaderData(4,Qt::Horizontal,"课程名称",Qt::DisplayRole);
    m_testModel->setHeaderData(5,Qt::Horizontal,"课程类型",Qt::DisplayRole);
    m_testModel->setHeaderData(6,Qt::Horizontal,"考试时间",Qt::DisplayRole);

    m_choiceCourseModel->setHeaderData(0,Qt::Horizontal,"编号",Qt::DisplayRole);
    m_choiceCourseModel->setHeaderData(1,Qt::Horizontal,"老师编号",Qt::DisplayRole);
    m_choiceCourseModel->setHeaderData(2,Qt::Horizontal,"老师名字",Qt::DisplayRole);
    m_choiceCourseModel->setHeaderData(3,Qt::Horizontal,"课程编号",Qt::DisplayRole);
    m_choiceCourseModel->setHeaderData(4,Qt::Horizontal,"课程名称",Qt::DisplayRole);
    m_choiceCourseModel->setHeaderData(5,Qt::Horizontal,"课程类型",Qt::DisplayRole);

    m_courseModel->setHeaderData(0,Qt::Horizontal,"编号",Qt::DisplayRole);
    m_courseModel->setHeaderData(1,Qt::Horizontal,"学生学号",Qt::DisplayRole);
    m_courseModel->setHeaderData(2,Qt::Horizontal,"学生名字",Qt::DisplayRole);
    m_courseModel->setHeaderData(3,Qt::Horizontal,"课程编号",Qt::DisplayRole);
    m_courseModel->setHeaderData(4,Qt::Horizontal,"课程名称",Qt::DisplayRole);
    m_courseModel->setHeaderData(5,Qt::Horizontal,"课程类型",Qt::DisplayRole);
    m_courseModel->setHeaderData(6,Qt::Horizontal,"老师编号",Qt::DisplayRole);
    m_courseModel->setHeaderData(7,Qt::Horizontal,"老师名字",Qt::DisplayRole);

    //获取表格数据
    m_evaModel->select();
    m_gradeModel->select();
    m_testModel->select();
    m_choiceCourseModel->select();
    m_courseModel->select();

    //绑定视图和模型
    ui->tvEva->setModel(m_evaModel);
    ui->tvGrade->setModel(m_gradeModel);
    ui->tvTest->setModel(m_testModel);
    ui->tvChoiceCourse->setModel(m_choiceCourseModel);
    ui->tvCourse->setModel(m_courseModel);

    //筛选数据
    for(int i = 0;i < m_evaModel->rowCount(); ++i){
        if(m_evaModel->data(m_evaModel->index(i,1)).toString() != m_account){
            ui->tvEva->hideRow(i);
        }
    }

    for(int i = 0;i < m_gradeModel->rowCount();++i){
        if(m_gradeModel->data(m_gradeModel->index(i,1)).toString() != m_account){
            ui->tvGrade->hideRow(i);
        }
    }

    for(int i = 0;i < m_testModel->rowCount(); ++i){
        if(m_testModel->data(m_testModel->index(i,1)).toString() != m_account){
            ui->tvTest->hideRow(i);
        }
    }

    for(int i = 0;i < m_courseModel->rowCount(); ++i){
        if(m_courseModel->data(m_courseModel->index(i,1)).toString() != m_account){
            ui->tvCourse->hideRow(i);
        }
    }

    //设置图表不可编辑
    ui->tvEva->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tvGrade->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tvTest->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tvChoiceCourse->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tvCourse->setEditTriggers(QAbstractItemView::NoEditTriggers);

    //初始化可评价老师列表
    //    for(int i = 0;i < m_gradeModel->rowCount(); ++i){
    //        if(ui->tvGrade->isRowHidden(i)){
    //            continue;
    //        }else{
    //            bool ok = false;
    //            for(int j = 0; j < ui->combTeacherEva->count(); ++j){
    //                if(ui->combTeacherEva->itemText(j).contains(m_gradeModel->data(m_gradeModel->index(i,8)).toString())){
    //                    ok = true;
    //                    break;
    //                }
    //            }
    //            if(!ok){
    //                ui->combTeacherEva->addItem(m_gradeModel->data(m_gradeModel->index(i,8)).toString());
    //            }
    //        }

    //    }

    //计算学分
    double credit = 0.0;
    for(int i = 0;i < m_gradeModel->rowCount(); ++i){
        if(ui->tvGrade->isRowHidden(i)){
            continue;
        }
        else{
            if(m_gradeModel->data(m_gradeModel->index(i,6)).toDouble() < 60.0){
                continue;
            }
            query.exec("select * from tb_course where id="+m_gradeModel->data(m_gradeModel->index(i,3)).toString()+";");
            if(query.size() == 0){
                QMessageBox::warning(0,"警告","课程不存在!");
                return;
            }
            query.first();
            credit += query.value("credit").toDouble();
        }
    }
    ui->labCredi->setText(QString::asprintf("%.2f",credit));

    query.exec("select * from tb_student where id="+m_account+";");
    if(query.size() == 0){
        QMessageBox::warning(0,"警告","学生信息查找失败!");
        return;
    }
    query.first();
    QString major = query.value("major").toString();

    query.exec("select * from tb_credit where name='"+major+"';");
    if(query.size() == 0){
        QMessageBox::warning(0,"警告","专业学分信息查找失败!");
        return;
    }
    query.first();
    ui->labTargetCredit->setText(query.value("credit").toString());


}
void studentui::on_checkBox_stateChanged(int arg1)
{
    ui->editName->setReadOnly(!arg1);
    ui->editPswd->setReadOnly(!arg1);
    ui->btnReset->setEnabled(arg1);
    ui->btnSubmit->setEnabled(arg1);

    QSqlQuery query;
    query.exec("select * from tb_student where id="+m_account+";");
    query.first();
    if(!query.isValid()){
        QMessageBox::warning(0,"警告","数据库检索出错!");
        return;
    }

    ui->labId->setText(m_account);
    ui->labStudent->setText(query.value("name").toString());
    ui->editPswd->setText(query.value("pswd").toString());
    ui->editName->setText(query.value("name").toString());
    ui->labMajor->setText(query.value("major").toString());
    ui->labClass->setText(query.value("class").toString());
    ui->labDegree->setText(query.value("degree").toString());
}


void studentui::on_btnReset_clicked()
{
    ui->editName->setReadOnly(true);
    ui->editPswd->setReadOnly(true);
    ui->btnReset->setEnabled(false);
    ui->btnSubmit->setEnabled(false);
    ui->checkBox->setChecked(false);

    QSqlQuery query;
    query.exec("select * from tb_student where id="+m_account+";");
    query.first();
    if(!query.isValid()){
        QMessageBox::warning(0,"警告","数据库检索出错!");
        return;
    }

    ui->labId->setText(m_account);
    ui->labStudent->setText(query.value("name").toString());
    ui->editPswd->setText(query.value("pswd").toString());
    ui->editName->setText(query.value("name").toString());
    ui->labMajor->setText(query.value("major").toString());
    ui->labClass->setText(query.value("class").toString());
    ui->labDegree->setText(query.value("degree").toString());
}


void studentui::on_btnSubmit_clicked()
{
    QSqlQuery query;
    if(query.exec("update tb_student set name='"+ui->editName->text()+"',pswd='"+ui->editPswd->text()+"' where id="+m_account+";")){
        QMessageBox::information(0,"提示","提交成功!");
        ui->btnReset->click();
        ui->labStudent->setText(ui->editName->text());
        return;
    }else{
        QMessageBox::warning(0,"警告","提交失败.");
    }
}


void studentui::on_pushButton_clicked()
{
    if(ui->tvCourse->currentIndex().isValid()){
        QSqlQuery query;
        if(query.exec("delete from tb_stuCourse where id="+m_courseModel->data(m_courseModel->index(ui->tvCourse->currentIndex().row(),0)).toString()+";")){
            QMessageBox::information(0,"提示","退课成功!");

            m_courseModel->select();
            for(int i = 0;i < m_courseModel->rowCount(); ++i){
                ui->tvCourse->showRow(i);
                if(m_courseModel->data(m_courseModel->index(i,1)).toString() != m_account){
                    ui->tvCourse->hideRow(i);
                }
            }
        }else{
            QMessageBox::warning(0,"警告","退课失败!");
        }
    }


}


//void studentui::on_btnSubmitEva_clicked()
//{
//    QSqlQuery query;
//    query.exec("select * from tb_teacher where name='"+ui->combTeacherEva->currentText()+"';");
//    query.first();
//    if(!query.isValid()){
//        return;
//    }

//    QString teacherId = query.value("id").toString();

//    if(query.exec("insert into tb_evaluate(studentid,studentname,teacherid,teachername,evalute,score)"
//                  " values("+m_account+",'"+ui->labStudent->text()+"',"+teacherId+",'"+ui->combTeacherEva->currentText()+"','"+ui->editEva->text()+"',"+ui->spinScoreEva->text()+");")){
//        QMessageBox::information(0,"提示","提交成功!");
//        m_evaModel->select();

//        //筛选数据
//        for(int i = 0;i < m_evaModel->rowCount(); ++i){
//            ui->tvEva->showRow(i);
//            if(m_evaModel->data(m_evaModel->index(i,1)).toString() != m_account){
//                ui->tvEva->hideRow(i);
//            }
//        }
//    }
//}


void studentui::on_pushButton_2_clicked()
{
    if(ui->tvChoiceCourse->currentIndex().isValid()){
        QString courseId = m_choiceCourseModel->data(m_choiceCourseModel->index(ui->tvChoiceCourse->currentIndex().row(),3)).toString();
        bool ok = false;
        for(int i = 0;i < m_courseModel->rowCount(); ++i){
            if(ui->tvCourse->isRowHidden(i)){
                continue;
            }
            else{
                if(m_courseModel->data(m_courseModel->index(i,3)).toString() == courseId){
                    ok = true;
                    break;
                }
            }
        }
        if(ok == false){
            QSqlQuery query;

            QString studentId = m_account;
            QString studentName = ui->labStudent->text();
            //courseid
            QString courseName = m_choiceCourseModel->data(m_choiceCourseModel->index(ui->tvChoiceCourse->currentIndex().row(),4)).toString();
            QString courseType = m_choiceCourseModel->data(m_choiceCourseModel->index(ui->tvChoiceCourse->currentIndex().row(),5)).toString();
            QString teacherId = m_choiceCourseModel->data(m_choiceCourseModel->index(ui->tvChoiceCourse->currentIndex().row(),1)).toString();
            QString teacherName = m_choiceCourseModel->data(m_choiceCourseModel->index(ui->tvChoiceCourse->currentIndex().row(),2)).toString();
            if(query.exec("insert into tb_stuCourse(studentid,studentname,courseid,coursename,coursetype,teacherid,teachername)"
                          " values("+studentId+",'"+studentName+"',"+courseId+",'"+courseName+"','"+courseType+"',"+teacherId+",'"+teacherName+"');")){
                QMessageBox::information(0,"提示","选课成功!");

                m_courseModel->select();
                for(int i = 0;i < m_courseModel->rowCount(); ++i){
                    ui->tvCourse->showRow(i);
                    if(m_courseModel->data(m_courseModel->index(i,1)).toString() != m_account){
                        ui->tvCourse->hideRow(i);
                    }
                }

            }else{
                QMessageBox::warning(0,"警告","选课失败!");
                return;
            }
        }
    }
}


void studentui::on_treeWidget_itemClicked(QTreeWidgetItem *item, int column)
{
    if(item->text(column) == "信息管理"){
        ui->stackedWidget->setCurrentWidget(ui->pagStudent);
    }else if(item->text(column) == "选课"){
        ui->stackedWidget->setCurrentWidget(ui->pagChoiceCourse);
    }else if(item->text(column) == "我的课程"){
        ui->stackedWidget->setCurrentWidget(ui->pagMyCourse);
    }else if(item->text(column) == "我的考试"){
        ui->stackedWidget->setCurrentWidget(ui->pagMyTest);
    }else if(item->text(column) == "我的成绩"){
        ui->stackedWidget->setCurrentWidget(ui->pagMyGrade);
    }else if(item->text(column) == "总结性评教"){
        ui->stackedWidget->setCurrentWidget(ui->pagMyEva);
    }
}


void studentui::on_btnEva_clicked()
{
    //可评价老师
    QStringList alist;
    for(int i = 0;i < m_gradeModel->rowCount(); ++i){
        if(ui->tvGrade->isRowHidden(i)){
            continue;
        }else{
            bool ok = false;
            for(int j = 0; j < alist.count(); ++j){
                if(alist.at(j).contains(m_gradeModel->data(m_gradeModel->index(i,8)).toString())){
                    ok = true;
                    break;
                }
            }
            if(!ok){
                alist.append(m_gradeModel->data(m_gradeModel->index(i,8)).toString());
            }
        }

    }


    evaui* e = new evaui(&m_db,m_account,ui->labStudent->text(),alist,this);
    int result = e->exec();
    if(result == QDialog::Accepted){
        m_evaModel->select();

        //筛选数据
        for(int i = 0;i < m_evaModel->rowCount(); ++i){
            ui->tvEva->showRow(i);
            if(m_evaModel->data(m_evaModel->index(i,1)).toString() != m_account){
                ui->tvEva->hideRow(i);
            }
        }
    }
}

